//
//  JsonDataManager.swift
//  MakeMoney
//
//  Created by Fan ZhiQiang on 2024/12/6.
//

import Foundation
class JsonDataManager {
    static let shared = JsonDataManager()
    private(set) var total: [Lottery] = []
    
    private init() {
        total = loadData2()
    }
    
    private func loadData2<T>() -> [T] where T: Codable, T: Comparable {
        print("loadData ---- ")
        guard let url = Bundle.main.url(forResource: "lotteryTicket", withExtension: "json") else {
            return []
        }
        do {
            let data = try Data(contentsOf: url)
            let decode = JSONDecoder()
            let loaded = try decode.decode([T].self, from: data)
            return loaded.sorted()
        } catch {
            print(error.localizedDescription)
        }
        return []
    }
}


func loadData<T>() -> [T] where T: Codable, T: Comparable {
    print("loadData2 ---- ")
    guard let url = Bundle.main.url(forResource: "lotteryTicket", withExtension: "json") else {
        return []
    }
    do {
        let data = try Data(contentsOf: url)
        let decode = JSONDecoder()
        let loaded = try decode.decode([T].self, from: data)
        return loaded.sorted()
    } catch {
        print(error.localizedDescription)
    }
    return []
}
let total: [Lottery] = loadData()




